<template>
    <!-- 最上面右边的窗口操作按钮 -->
    <div class="win-op no-drag">
        <!-- 置顶按钮 -->
        <div v-if="showSetTop" :class="['iconfont', isTop ? 'icon-quxiaozhiding' : 'icon-zhiding']" @click="top"
            :title="isTop ? '取消置顶' : '置顶'"></div>
        <!-- 最小化按钮 -->
        <div v-if="showMin" class="iconfont icon-zuixiaohua2" @click="minimizer" title="最小化"></div>
        <!-- 最大化按钮 -->
        <div v-if="showMax" :class="['iconfont', isMax ? 'icon-zuixiaohua' : 'icon-jurassic_Window-max']"
            @click="maximizer" :title="isMax ? '还原窗口大小' : '最大化窗口'">
        </div>
        <!-- 关闭按钮 -->
        <div v-if="showClose" :class="['iconfont icon-close', closeType == 1 ? 'icon-zuixiaohua1' : 'icon-guanbi']"
            @click="close" title="关闭">
        </div>
    </div>
</template>

<script setup>
import { ref, reactive, getCurrentInstance, nextTick, onMounted } from "vue"
const { proxy } = getCurrentInstance();

/* 使用props让父组件可以使用这些响应式数据设置是否有这个按钮 */
const props = defineProps({
    showSetTop: {
        type: Boolean,
        default: true,
    },
    showMin: {
        type: Boolean,
        default: true,

    },
    showMax: {
        type: Boolean,
        default: true,

    },
    showClose: {
        type: Boolean,
        default: true,

    },
    /* 关闭窗口的类型：0：关闭程序 1：隐藏到托盘 */
    closeType: {
        type: Number,
        default: 1,
    },
})

const isTop = ref(false)
const isMax = ref(false)

onMounted(() => {
    isMax.value = false
    isTop.value = false
})

/* 包装向主进程通信的代码逻辑 */
const winOp = (action, data) => {
    myApi.winTitleOp({ action, data })
}

/* 关闭窗口的时候需要回调执行关闭的数据处理等操作 */
const emit = defineEmits(["closeCallback"])

const close = () => {
    winOp('close', { closeType: props.closeType })
    emit("closeCallback")
}

const minimizer = () => {
    winOp('minimize', {})
}

const maximizer = () => {
    if (isMax.value) {
        winOp('unmaximize', {})
    } else {
        winOp('maximize', {})
    }
    isMax.value = !isMax.value
}

const top = () => {
    isTop.value = !isTop.value
    winOp('top', { isTop: isTop.value })
}

</script>

<style lang="scss" scoped>
.win-op {
    top: 0px;
    right: 0px;
    position: absolute;
    z-index: 1;
    overflow: hidden;
    border-radius: 0px 3px 0px 0px;

    .iconfont {
        float: left;
        font-size: 18px;
        color: #101010;
        text-align: center;
        display: flex;
        justify-content: center;
        cursor: pointer;
        height: 25px;
        align-items: center;
        padding: 0px 10px;

        &:hover {
            background: #b8e7fd;
        }
    }

    .icon-close {
        &:hover {
            background: #fb7373;
            color: #fff;
        }
    }

    .win-top {
        background: #ddd;
        color: #07c160;
    }
}
</style>
